1 =============================================================================
2 APPLICATION : CSSLTreeViewCRUDDragDrop Project Overview
3 =============================================================================
5 /////////////////////////////////////////////////////////////////////////////
8 This Application showcases a custom TreeView with added functionalities of
9 CRUD and Drag-And-Drop operations
11 /////////////////////////////////////////////////////////////////////////////
14 You must have the following components installed on your machine
15 1) Microsoft Visual Studio 2010
16 2) Microsoft Silverlight 4 SDK
17 3) Microsoft Silverlight 4 Toolkit April 2010
19 /////////////////////////////////////////////////////////////////////////////
22 The following steps walk through a demonstration of the sample.
24 Step1. Build the sample project in Visual Studio 2010,
26 Step2. Start the Application by selecting "Start Debugging" or "Start without Debugging" in the build menu.
28 Step3. Right-Click in the box and select "Add" in the context menu. Add more root nodes and also child nodes to existing nodes.
30 Step4. Right-Click a node, and select "Edit". Edit the content of the node.
32 Step5. Right-Click a node, and select "Delete". This will delete the node.
34 Step6. Select any child node, drag and drop it to any other node.
36 Step7. Close the application
38 /////////////////////////////////////////////////////////////////////////////
41 Step1. Create a new Silverlight Application called CSSLTreeViewCRUDDragDrop
43 Step2. Add a new class Node. Replace the code with this
46 /// Data bound to tree view
50 #region Private Members
53 /// Text to display in each tree view item
57 /// Children of tree view item
59 private ObservableCollection<Node> children;
62 /// Event Handler for PropertyChanged Event
64 public event PropertyChangedEventHandler PropertyChanged;
68 #region Public Properties
71 /// Gets or sets the Children of node
73 public ObservableCollection<Node> Children
75 get { return children; }
76 set { children = value; }
80 /// Gets or sets the Text of node
83 { get { return text; } set { text = value; } }
90 /// Creates a new instance of Node
92 /// <param name="text"></param>
93 public Node(String text)
95 Children = new ObservableCollection<Node>();
101 #region Public Methods
104 /// Adds a child lnode
106 /// <param name="node">Node to be added</param>
107 public void Add(Node node)
110 NotifyPropertyChanged("Children");
114 /// Deletes a child node
116 /// <param name="node">Node to be deleted</param>
117 public void Delete(Node node)
119 children.Remove(node);
120 NotifyPropertyChanged("Children");
125 #region Private Methods
128 /// Event handler for PropertyChange
130 /// <param name="info"></param>
131 private void NotifyPropertyChanged(String info)
133 if (PropertyChanged != null)
135 PropertyChanged(this, new PropertyChangedEventArgs(info));
142 Step3. Add a new Silverlight User Control called TreeViewCRUDDragDrop
144 Replace the code in TreeViewCrudDragDrop.xaml by the following code
147 xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
148 x:Class="CSSLTreeViewCRUDDragDrop.TreeViewCrudDragDrop"
149 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
150 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
151 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
152 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
154 d:DesignHeight="300" d:DesignWidth="400"
155 xmlns:toolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit"
156 xmlns:mswindows="clr-namespace:Microsoft.Windows;assembly=System.Windows.Controls.Toolkit">
158 <UserControl.Resources>
159 <!-- Template for Edit mode of TreeViewItem -->
160 <sdk:HierarchicalDataTemplate x:Key="TreeViewMainEditTemplate"
161 ItemsSource="{Binding Children}">
162 <TextBox Text="{Binding Text,Mode=TwoWay}" >
164 </sdk:HierarchicalDataTemplate>
165 <!-- Template for Read mode for TreeViewItem -->
166 <sdk:HierarchicalDataTemplate x:Key="TreeViewMainReadTemplate"
167 ItemsSource="{Binding Children}">
168 <TextBlock Text="{Binding Text,Mode=TwoWay}"
169 MouseRightButtonDown="TreeViewMain_MouseRightButtonDown"
170 MouseRightButtonUp="TreeViewMain_MouseRightButtonUp"
171 MouseLeftButtonDown="TreeViewMain_MouseLeftButtonDown" >
173 </sdk:HierarchicalDataTemplate>
174 </UserControl.Resources>
176 <Grid x:Name="LayoutRoot" Background="White">
177 <!-- TreeViewDragDropTarget from Toolkit to add DragAndDrop feature -->
178 <toolkit:TreeViewDragDropTarget AllowDrop="True">
179 <!-- Custom TreeView -->
180 <sdk:TreeView Name="TreeViewMain"
181 ItemTemplate="{StaticResource TreeViewMainReadTemplate}"
182 MouseRightButtonDown="TreeViewMain_MouseRightButtonDown"
183 MouseRightButtonUp="TreeViewMain_MouseRightButtonUp"
184 MouseLeftButtonDown="TreeViewMain_MouseLeftButtonDown"
185 Width="400" Height="400" >
187 </toolkit:TreeViewDragDropTarget>
189 <!-- Context Menu -->
191 <Popup Name="ContextMenu" Visibility="Collapsed">
192 <Border BorderThickness="1" BorderBrush="Black" Background="White">
194 <HyperlinkButton Content="Add" Name="AddButton"
195 Click="AddButton_Click" />
196 <HyperlinkButton Content="Edit" Name="EditButton"
197 Click="EditButton_Click"/>
198 <HyperlinkButton Content="Delete" Name="DeleteButton"
199 Click="DeleteButton_Click"/>
208 Replace the code in TreeViewCrudDragDrop.xaml.cs with the following code
211 /// Code Behind of Custom Silverlight User Control which implements
212 /// a TreeView with added functionalities of CRUD and Drag-And-Drop
214 public partial class TreeViewCrudDragDrop : UserControl
216 #region Member Variables
219 /// Collection bound to TreView
221 ObservableCollection<Node> objectTree;
224 /// Data bound to currently selected TreeViewItem
233 /// Gets or sets the data bound to TreeView
235 public List<Node> Items
239 return objectTree.ToList<Node>();
243 objectTree = new ObservableCollection<Node>(value);
244 TreeViewMain.ItemsSource = objectTree;
253 /// Creates a new instance of TreeViewCrudDragDrop
255 public TreeViewCrudDragDrop()
257 InitializeComponent();
258 objectTree = new ObservableCollection<Node>();
259 TreeViewMain.ItemsSource = objectTree;
264 #region Event Handlers
267 /// Event handler for MouseRightButtonDown of TreeView and TreeViewItem
269 /// <param name="sender">Object on which event occurred</param>
270 /// <param name="e">Event Arguements for the event</param>
271 private void TreeViewMain_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
273 DisableEditForSelectedItem();
279 /// Event handler for MouseRightButtonUp of TreeView and TreeViewItem
281 /// <param name="sender">Object on which event occurred</param>
282 /// <param name="e">Event Arguements for the event</param>
283 private void TreeViewMain_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
285 DisableEditForSelectedItem();
287 if (sender is TextBlock)
289 selectedNode = (Node)((sender as TextBlock).DataContext);
300 /// Event handler for MouseLeftButtonDown of TreeView and TreeViewItem
302 /// <param name="sender">Object on which event occurred</param>
303 /// <param name="e">Event Arguements for the event</param>
304 private void TreeViewMain_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
306 DisableEditForSelectedItem();
312 /// Event handler for Add Button Click Event
314 /// <param name="sender">Add Button</param>
315 /// <param name="e">Event arguements for event</param>
316 private void AddButton_Click(object sender, RoutedEventArgs e)
318 Node newNode = new Node("New Node");
320 if (selectedNode != null)
322 selectedNode.Add(newNode);
326 if (objectTree != null)
328 objectTree.Add(newNode);
332 objectTree = new ObservableCollection<Node>();
333 objectTree.Add(newNode);
341 /// Event handler for Edit Button Click Event
343 /// <param name="sender">Edit Button</param>
344 /// <param name="e">Event arguements for event</param>
345 private void EditButton_Click(object sender, RoutedEventArgs e)
347 EnalbleEditForSelectedItem();
349 TreeViewItem selectedTreeViewItem =
350 TreeViewExtensions.GetContainerFromItem(TreeViewMain, selectedNode);
356 /// Event handler for Delete Button Click Event
358 /// <param name="sender">Delete Button</param>
359 /// <param name="e">Event arguements for event</param>
360 private void DeleteButton_Click(object sender, RoutedEventArgs e)
362 TreeViewItem selectedTreeViewItem =
363 TreeViewExtensions.GetContainerFromItem(TreeViewMain, selectedNode);
365 if (selectedTreeViewItem != null)
367 TreeViewItem selectedTreeViewItemParent =
368 TreeViewExtensions.GetParentTreeViewItem(selectedTreeViewItem);
370 if (selectedTreeViewItemParent != null)
372 Node seleactedParentNode = (Node)selectedTreeViewItemParent.DataContext;
373 seleactedParentNode.Delete(selectedNode);
377 objectTree.Remove(selectedNode);
389 /// Show context menu
391 /// <param name="e">Mouse Button Event Arguements for getting cursor position</param>
392 private void ShowContextMenu(MouseButtonEventArgs e)
395 Point p = e.GetPosition(null);
396 ContextMenu.Visibility = Visibility.Visible;
397 ContextMenu.IsOpen = true;
398 ContextMenu.SetValue(Canvas.LeftProperty, (double)p.X);
399 ContextMenu.SetValue(Canvas.TopProperty, (double)p.Y);
403 /// Hide context menu
405 private void HideContextMenu()
407 ContextMenu.Visibility = Visibility.Collapsed;
408 ContextMenu.IsOpen = false;
412 /// Enable Edit Mode for selected TreeViewItem
414 private void EnalbleEditForSelectedItem()
416 if (selectedNode != null)
418 SetTemplateForSelectedItem("TreeViewMainEditTemplate");
423 /// Disable Edit mode for selected TreeViewItem
425 private void DisableEditForSelectedItem()
427 if (selectedNode != null)
429 SetTemplateForSelectedItem("TreeViewMainReadTemplate");
435 /// Set Template for Selected TreeViewItem
437 /// <param name="templateName">Template Name</param>
438 private void SetTemplateForSelectedItem(String templateName)
440 HierarchicalDataTemplate hdt = (HierarchicalDataTemplate)Resources[templateName];
442 TreeViewItem selectedTreeViewItem =
443 TreeViewExtensions.GetContainerFromItem(TreeViewMain, selectedNode);
445 if (selectedTreeViewItem != null)
446 selectedTreeViewItem.HeaderTemplate = hdt;
452 Step3. Add instance of TreeViewCrudDragDrop in MainPage
454 Add the following attribute in the UserControl attribute of MainPage
456 xmlns:Crud="clr-namespace:CSSLTreeViewCRUDDragDrop"
458 Add the following code inside the grid tag
460 <Crud:TreeViewCrudDragDrop />
462 /////////////////////////////////////////////////////////////////////////////
465 MichaelSnow: Silverlight Tip of the Day #3 – Mouse Right Clicks
466 http://www.michaelsnow.com/2010/04/23/silverlight-tip-of-the-day-3-mouse-right-clicks/
468 MSDN: DataBinding Silverlight
469 http://msdn.microsoft.com/en-us/library/cc278072(v=vs.95).aspx
471 Codeplex: Silverlight Toolkit
472 http://timheuer.com/blog/archive/2009/10/19/silverlight-toolkit-adds-drag-drop-support.aspx
474 /////////////////////////////////////////////////////////////////////////////